package com.server;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import com.game.base.SimulationObject;
import com.game.database.MysqlHelper;
import com.game.manager.SobManager;

public class User implements Observer, Serializable {

	private static final long serialVersionUID = -8050208270607902148L;

	private static final Map<Object, User> users = new HashMap<Object, User>();
	
	private static final Map<Integer, User> mirrorToUsers = new HashMap<Integer, User>();
	
	public int id;
	
	public String account;
	
	public long time;
	
	public int ctrl;
	
	public int mirror;
	
	public static User getUserByMirror(int mirrorid){
		Object[] params = new Object[1];
		params[0] = mirrorid;
		User user = null;
		if(mirrorToUsers.containsKey(mirrorid)){
			return mirrorToUsers.get(mirrorid);
		}
		try {
			HashMap<String, Object> obj = MysqlHelper.ExecuteRow("select * from sys_user where mirror = ?",params);
			user = new User();
			user.id = (Integer)obj.get("id");
			user.account = (String)obj.get("account");
			user.time = (Integer)obj.get("time");
			user.ctrl = (Integer)obj.get("ctrl");
			user.mirror = (Integer)obj.get("mirror");
			mirrorToUsers.put(mirrorid,user);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return user;
	}
	
	public static User getUser(int id){
		Object[] params = new Object[1];
		params[0] = id;
		User user = null;
		if(users.containsKey(id)){
			return users.get(id);
		}
		try {
			HashMap<String, Object> obj = MysqlHelper.ExecuteRow("select * from sys_user where id = ?",params);
			user = new User();
			user.id = id;
			user.account = (String)obj.get("account");
			user.time = (Integer)obj.get("time");
			user.ctrl = (Integer)obj.get("ctrl");
			user.mirror = (Integer)obj.get("mirror");
			users.put(id, user);
			users.put(user.account, user);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return user;
	}
	
	public static User getUser(String account){
		Object[] params = new Object[1];
		params[0] = account;
		User user = null;
		if(users.containsKey(account)){
			return users.get(account);
		}
		try {
			HashMap<String, Object> obj = MysqlHelper.ExecuteRow("select * from sys_user where `account` = ?",params);
			if(obj==null)
				return null;
			else{
				user = new User();
				user.id = (Integer)obj.get("id");
				user.account = (String)obj.get("account");
				user.time = (Integer)obj.get("time");
				user.ctrl = (Integer)obj.get("ctrl");
				user.mirror = (Integer)obj.get("mirror");
				users.put(account, user);
				users.put(user.id, user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		return user;
	}
	
	public static boolean checkLogin(String account){
		Object[] ps = new Object[1];
		ps[0] = account;
		try {
			return (Integer)MysqlHelper.ExecuteScalar("select ctrl from sys_user where account = ?", ps)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	public static User createUser(String account){
		Object[] p = new Object[2];
		p[0] = account;
		Integer mirror = SobManager.get().createSob(SimulationObject.class.getName(), SimulationObject.ROOT).id;
		p[1] = mirror;
		String sql = "insert into sys_user (`account`,`time`,`mirror`) values (?,unix_timestamp(),?)";
		try {
			int id = MysqlHelper.ExecuteInsert(sql,p);
			return getUser(id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public User(){
		
	}
	
	public void updateAttribute(String attr){
		try {
			Field field = User.class.getDeclaredField(attr);
			Object[] params = new Object[1];
			params[0] = field.get(this);
			MysqlHelper.ExecuteNoneQuery("update sys_user set "+attr+" = ?  where id = "+this.id, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void update(Observable actor, Object arg1) {

	}

}
